<template>
  <div>
    <button @click="savePageAsImage">保存页面为图片</button>
  </div>
</template>
 
<script>
import html2canvas from 'html2canvas';
 
export default {
  methods: {
    async savePageAsImage() {
      try {
        const element = document.body; // 要保存为图片的页面元素
        const canvas = await html2canvas(element);
        const img = canvas.toDataURL('image/png');
        const imgElement = document.createElement('img');
        imgElement.src = img;
 
        // 创建下载链接
        const a = document.createElement('a');
        a.href = img;
        a.download = '页面截图.png';
        a.click();
      } catch (error) {
        console.error('Error saving page as image:', error);
      }
    },
  },
};
</script>